#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
下载全国行政区划数据
作者：Wu.Liang
日期：2025-01-27
功能：从china-division项目下载最新的全国行政区划数据
"""

import urllib.request
import json
import os
from datetime import datetime

def download_region_data():
    """下载全国行政区划数据"""
    print("开始下载全国行政区划数据...")
    
    # china-division项目的JSON数据URL
    url = "https://raw.githubusercontent.com/modood/Administrative-divisions-of-China/master/dist/pcas-code.json"
    
    try:
        print(f"正在从 {url} 下载数据...")
        response = urllib.request.urlopen(url)
        data = response.read().decode('utf-8')
        
        # 解析JSON数据
        region_data = json.loads(data)
        
        # 保存原始数据
        with open("AI_Task/区域管理测试数据补充_执行文档/china_division_raw.json", 'w', encoding='utf-8') as f:
            json.dump(region_data, f, ensure_ascii=False, indent=2)
        
        print("原始数据已保存到: china_division_raw.json")
        return region_data
        
    except Exception as e:
        print(f"下载失败: {e}")
        print("使用备用数据...")
        return get_backup_region_data()

def get_backup_region_data():
    """备用数据（当网络下载失败时使用）"""
    return {
        "86": {
            "name": "中华人民共和国",
            "children": {
                "110000": {
                    "name": "北京市",
                    "children": {
                        "110100": {
                            "name": "北京市",
                            "children": {
                                "110101": {
                                    "name": "东城区",
                                    "children": {
                                        "110101001": {"name": "东华门街道"},
                                        "110101002": {"name": "景山街道"},
                                        "110101003": {"name": "交道口街道"},
                                        "110101004": {"name": "安定门街道"},
                                        "110101005": {"name": "北新桥街道"},
                                        "110101006": {"name": "东四街道"},
                                        "110101007": {"name": "朝阳门街道"},
                                        "110101008": {"name": "建国门街道"},
                                        "110101009": {"name": "东直门街道"},
                                        "110101010": {"name": "和平里街道"},
                                        "110101011": {"name": "前门街道"},
                                        "110101012": {"name": "崇文门外街道"},
                                        "110101013": {"name": "东花市街道"},
                                        "110101014": {"name": "龙潭街道"},
                                        "110101015": {"name": "体育馆路街道"},
                                        "110101016": {"name": "天坛街道"},
                                        "110101017": {"name": "永定门外街道"}
                                    }
                                },
                                "110102": {
                                    "name": "西城区",
                                    "children": {
                                        "110102001": {"name": "西长安街街道"},
                                        "110102002": {"name": "新街口街道"},
                                        "110102003": {"name": "月坛街道"},
                                        "110102004": {"name": "展览路街道"},
                                        "110102005": {"name": "德胜街道"},
                                        "110102006": {"name": "金融街街道"},
                                        "110102007": {"name": "什刹海街道"},
                                        "110102008": {"name": "大栅栏街道"},
                                        "110102009": {"name": "天桥街道"},
                                        "110102010": {"name": "椿树街道"},
                                        "110102011": {"name": "陶然亭街道"},
                                        "110102012": {"name": "广安门内街道"},
                                        "110102013": {"name": "牛街街道"},
                                        "110102014": {"name": "白纸坊街道"},
                                        "110102015": {"name": "广安门外街道"}
                                    }
                                }
                            }
                        }
                    }
                },
                "310000": {
                    "name": "上海市",
                    "children": {
                        "310100": {
                            "name": "上海市",
                            "children": {
                                "310101": {
                                    "name": "黄浦区",
                                    "children": {
                                        "310101001": {"name": "南京东路街道"},
                                        "310101002": {"name": "外滩街道"},
                                        "310101003": {"name": "半淞园路街道"},
                                        "310101004": {"name": "小东门街道"},
                                        "310101005": {"name": "豫园街道"},
                                        "310101006": {"name": "老西门街道"},
                                        "310101007": {"name": "五里桥街道"},
                                        "310101008": {"name": "打浦桥街道"},
                                        "310101009": {"name": "淮海中路街道"},
                                        "310101010": {"name": "瑞金二路街道"}
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

def main():
    """主函数"""
    print("=" * 60)
    print("全国行政区划数据下载工具")
    print("作者：Wu.Liang")
    print("日期：", datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    print("=" * 60)
    
    # 下载数据
    region_data = download_region_data()
    
    print(f"数据下载完成，共包含 {len(region_data)} 个顶级区域")
    print("数据已保存到: china_division_raw.json")

if __name__ == "__main__":
    main() 